package InternetCode.Socket.Exa6_1;

import java.net.*;
import java.util.Arrays;

public class ServerSocketDemo {

    //声明ServerSocket对象
    private ServerSocket server;
    //声明Socket对象socket
    private Socket socket;

    public void getServer() {
        try {
            // 根据端口号实例化服务器Socket对象
            server = new ServerSocket(1978);
            System.out.println("服务器套接字已经创建成功\n");
            //循环等待套接字连接
            while (true) {
                System.out.println("等待客户机的连接......\n");
                socket = server.accept();
                System.out.println(socket.getInetAddress().getHostAddress() +"连接成功......\n");
            }
        } catch (Exception e) {
            e.printStackTrace(); // 输出异常信息
        }
    }

    // 主方法
    public static void main(String[] args) {
        // 创建本类对象
        ServerSocketDemo frame = new ServerSocketDemo();
        // 调用方法创建服务器套接字
        frame.getServer();
    }
}
